package collectionFrame.Demo7_LinkedListExample;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class WorldCup {

    //初始化所有届的冠军队伍
    static List<WinnerPerYear> list_winner = new LinkedList<>();

    /**
     * 初始化所有届的冠军队伍
     */
    public static void initAllTeam(){

        list_winner.add(new WinnerPerYear(1,1930,"乌拉圭"));
        list_winner.add(new WinnerPerYear(2,1934,"意大利"));
        list_winner.add(new WinnerPerYear(3,1938,"意大利"));
        list_winner.add(new WinnerPerYear(4,1950,"乌拉圭"));
        list_winner.add(new WinnerPerYear(5,1954,"德国"));
        list_winner.add(new WinnerPerYear(6,1958,"巴西"));
        list_winner.add(new WinnerPerYear(7,1962,"巴西"));
        list_winner.add(new WinnerPerYear(8,1966,"英格兰"));
        list_winner.add(new WinnerPerYear(9,1970,"巴西"));
        list_winner.add(new WinnerPerYear(10,1974,"德国"));
        list_winner.add(new WinnerPerYear(11,1978,"阿根廷"));
        list_winner.add(new WinnerPerYear(12,1982,"意大利"));
        list_winner.add(new WinnerPerYear(13,1986,"阿根廷"));
        list_winner.add(new WinnerPerYear(14,1990,"德国"));
        list_winner.add(new WinnerPerYear(15,1994,"巴西"));
        list_winner.add(new WinnerPerYear(16,1998,"法国"));
        list_winner.add(new WinnerPerYear(17,2002,"巴西"));
        list_winner.add(new WinnerPerYear(18,2006,"意大利"));

    }

    /**
     * 通过给定的年份返回相应的冠军队伍
     * @param year
     * @return
     */
    public static String getCountryByYear(int year){

        //初始化返回的内容(国家或没找到)
        String backInfo = "";
        //作为是否找到该年份的标识
        boolean flag = true;

        for (WinnerPerYear winnerPerYear : list_winner) {
            if(winnerPerYear.getYear() == year){
                backInfo = winnerPerYear.getCountry();
                flag = false;
                break;
            }
        }
        if(flag){
            backInfo = "没有当前年份的信息";
        }

        return backInfo;
    }

    /**
     * 通过给定的国家，获取相应的冠军年份
     * @param country
     * @return
     */
    public static String getYearByCountry(String country){

        //初始化一个国家的所有获奖年份的列表
        List<Integer> list_year = new LinkedList<>();
        StringBuffer backInfo = new StringBuffer();

        //遍历所有的冠军信息，如果能匹配上国家，获取相应年份
        for (WinnerPerYear winnerPerYear : list_winner) {
            if(winnerPerYear.getCountry().equals(country)){
                list_year.add(winnerPerYear.getYear());
            }
        }

        //如果列表不为空，表示找到信息
        if(null != list_year && list_year.size()>0){
            Iterator<Integer> iterator = list_year.iterator();
            while(iterator.hasNext()){
                backInfo.append(iterator.next()+" ");
            }
        }else{
            //如果列表为空，表示没有找到信息
            backInfo.append("没有找到当前国家的获奖信息");
        }

        return backInfo.toString();

    }

    public static void main(String[] args) {
        WorldCup.initAllTeam();
        System.out.println(WorldCup.getCountryByYear(1972));
        System.out.println(WorldCup.getYearByCountry("巴西"));
    }

}

/**
 * 自定义一个每届世界杯的冠军的类
 */
class WinnerPerYear{

    private int period;
    private int year;
    private String country;


    public WinnerPerYear(int period, int year, String country) {
        this.period = period;
        this.year = year;
        this.country = country;
    }

    public int getPeriod() {
        return period;
    }

    public void setPeriod(int period) {
        this.period = period;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}
